#include "transiver.h"
#include <iostream>

/**
 * @brief 构造函数
 * @param username 用户名，应使用client创建的用户名
 * @param socket   同样应使用client创建的socket
 * @param select   在对战房间界面(待创建)确定该client是否为植物方
 *
 */
Transiver::Transiver(QString username, QTcpSocket* socket, int select){
    this->username = username;
    this->socket = socket;
    this->select = select;
}

Transiver::~Transiver()
{

}

/**
 * @brief 发送控制指令函数
 * @param xpos     放置目标物的x坐标
 * @param ypos     放置目标物的y坐标
 * @param select   放置目标物的种类(豌豆射手/普通僵尸)
 */
void Transiver::sendMsg(int xpos, int ypos, QString type){
        QJsonObject sendObj = {
            {"control", 1},
            {"msg", NULL},
            {"username", username},
            {"select", select},
            {"xpos",xpos},{"ypos", ypos},
            {"type", type}
    };
    QJsonDocument document=QJsonDocument(sendObj);
    QByteArray array = document.toJson();
    array.append('^');
    if(socket->isValid()){
        socket->write(array);
    }
}

/**
 * @brief 发送聊天信息
 * @param msg   聊天信息
 */
void Transiver::sendMsg(QString msg){
    QJsonObject sendObj;
    sendObj.insert("control", 2);
    sendObj.insert("msg", msg);
    sendObj.insert("username", username);
    sendObj.insert("select", select);
    sendObj.insert("xpos", NULL);
    sendObj.insert("ypos", NULL);
    sendObj.insert("type", NULL);
    QJsonDocument document=QJsonDocument(sendObj);
    QByteArray array = document.toJson();
    array.append('^');
    if(socket->isValid()){
        socket->write(array);
    }
    array.resize(0);
}

void Transiver::sendMsg(int select)
{
    QJsonObject sendObj;
    sendObj.insert("control", 3);
    sendObj.insert("msg", NULL);
    sendObj.insert("username", username);
    sendObj.insert("select", select);
    sendObj.insert("xpos", NULL);
    sendObj.insert("ypos", NULL);
    sendObj.insert("type", NULL);
    QJsonDocument document=QJsonDocument(sendObj);
    QByteArray array = document.toJson();
    array.append('^');
    if(socket->isValid()){
        socket->write(array);
    }
    array.resize(0);
}

void Transiver::sendStopMsg()
{
    QJsonObject sendObj;
    sendObj.insert("control", 5);
    sendObj.insert("msg", NULL);
    sendObj.insert("username", NULL);
    sendObj.insert("select", NULL);
    sendObj.insert("xpos", NULL);
    sendObj.insert("ypos", NULL);
    sendObj.insert("type", NULL);
    QJsonDocument document=QJsonDocument(sendObj);
    QByteArray array = document.toJson();
    array.append('^');
    if(socket->isValid()){
        socket->write(array);
    }
    array.resize(0);
}


void Transiver::parse(QByteArray jsonByte){
    QJsonDocument doc = QJsonDocument::fromJson(jsonByte);
    QJsonObject recv_obj = doc.object();
    if(recv_obj.value("control").toInt() == 1){
        int select = recv_obj.value("select").toInt();
        int xpos = recv_obj.value("xpos").toInt();
        int ypos = recv_obj.value("ypos").toInt();
        QString type = recv_obj.value("type").toString();
        emit broadCast(select, xpos, ypos, type);
    }
    else if(recv_obj.value("control").toInt() == 2){
        QString username = recv_obj.value("username").toString();
        QString msg = recv_obj.value("msg").toString();
        emit broadCast(username, msg);
    }
    else if(recv_obj.value("control").toInt() == 3){
        int select = recv_obj.value("select").toInt();
        QString username = recv_obj.value("username").toString();
        if(QString::compare(username, this->username) == 0) this->select = select;
        emit broadCast(select, username);
    }
    else if(recv_obj.value("control").toInt() == 4)
    {
        emit GameStart();
    }
    else if(recv_obj.value("control").toInt() == 5)
    {
        emit GameStop();
    }
}
